package com.example.manager.service;

import com.example.common.Ov.BorrowOv;
import com.example.common.entity.PageParam;
import com.example.common.entity.Rsp;
import com.example.common.pojo.Borrow;
import com.example.manager.mapper.BorrowMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class BorrowService {

    @Autowired
    private BorrowMapper borrowMapper;

    /**
     * 获取借款的列表
     *
     * @param pageParam
     * @return
     */
    public Rsp getAllBorrow(PageParam<BorrowOv> pageParam) {
        if (!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())) {
            PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());
            List<BorrowOv> borrowOvs = borrowMapper.superSearch(pageParam.getSuperSearchKeyWord());
            PageInfo<BorrowOv> pageInfo = new PageInfo<>(borrowOvs);
            return new Rsp(20000, pageInfo, "列表获取成功");
        } else {
            PageHelper.startPage(pageParam.getPageNo(), pageParam.getPageSize());
            List<BorrowOv> allList = borrowMapper.getAllList();
            PageInfo<BorrowOv> pageInfo = new PageInfo<>(allList);
            return new Rsp(20000, pageInfo, "列表获取成功");
        }
    }

    /**
     * 通过借款申请
     *
     * @param borrow
     * @return
     */
    public Rsp updateBorrow(Borrow borrow) {
        borrow.setStatus(1);
        Date date = new Date();
        borrow.setBorrowStartTime(date);
        int i = borrowMapper.updateByPrimaryKeySelective(borrow);
        if(i>0){
            return new Rsp(20000,"审核成功");
        }else {
            return new Rsp(50000,"审核失败");
        }
    }

    /**
     * 借款申请驳回
     * @param borrow
     * @return
     */
    public Rsp updateBorrowBack(Borrow borrow) {
        borrow.setStatus(5);

        int i = borrowMapper.updateByPrimaryKeySelective(borrow);
        if(i>0){
            return new Rsp(20000,"审核驳回成功");
        }else {
            return new Rsp(50000,"审核驳回失败");
        }
    }


    public Rsp updateBorrowEndTime(Borrow borrow){
        System.out.println(borrow.getBorrowEndTime());
        int i = borrowMapper.updateByPrimaryKeySelective(borrow);
        if(i>0){
            return new Rsp(20000,"截止时间设置成功");
        }else {
            return new Rsp(50000,"截止时间设置失败失败");
        }
    }
}
